import { _decorator, AudioSource, Component, Label, Node, tween, UIOpacity, v3, Vec3, warn } from 'cc';
import { Battle } from './Battle';
import { Globals } from './Globals';
import { Player } from './Player/Player';
import { ResourcesUtil } from './ResourcesUtil';
import { Enum_AudioClip } from './Enum';
import StorageManager from './Data/StorageManager';
const { ccclass, property } = _decorator;

@ccclass('Cure')
export class Cure extends Component {
    @property(Node) hpText: Node = null; // 血量文本
    // 恢复的血量
    cureHp = 10;
    // 持续时间
    duration = 3;
    private _audioSound: AudioSource;
    onLoad(): void {
        this._audioSound = this.node.getComponent(AudioSource);
        ResourcesUtil.loadAudioClip(Enum_AudioClip.Cure).then((clip) => {
            this._audioSound.clip = clip;
        })
    }
    onEnable() {
        // 播放声音
        try {
            StorageManager.instance.sound && this._audioSound.play();
        } catch (err) {
            warn(err)
        }
        // 恢复血量
        Battle.playerNode.getComponent(Player).hp += this.cureHp;
        // 显示血量
        this.hpText.getComponent(Label).string = `+${this.cureHp}`;
    }
    protected start(): void {

        // tween(this.hpText).to(1, { position: v3(0, 50, 1) }).start(); // 向上移动
    }

    update(deltaTime: number) {

        // 随着玩家
        if (Battle.playerNode) {
            this.node.setWorldPosition(Battle.playerNode.getWorldPosition());
        }
        // 持续时间减少
        this.duration -= deltaTime;
        if (this.duration < 0) {
            this._audioSound.stop();//停止播放声音
            Globals.putNode(this.node);
        }
    }
}


